const {
    wechatAPIS
} = require('../../tools/wechatApi.js');
const {
    Mysql
} = require('../../tools/index');
module.exports = async ctx => {
    let {
        orderId,
        total_fee,
        refund_fee
    } = ctx.query;
    try {
        let out_refund_no = createNo();
        let refundInfo = await wechatAPIS.refund(orderId, out_refund_no, total_fee, refund_fee);
        if (refundInfo.return_code == 'SUCCESS') {
            let sql = `UPDATE orderList SET out_refund_no = '${out_refund_no}',total_fee='${total_fee}',refund_fee='${refund_fee}',refund_info='${JSON.stringify(refundInfo)}' WHERE orderId = '${orderId}'`;
            await Mysql(sql);
            ctx.state.data = {
                code: '001',
                data: refundInfo
            }
        }
    } catch (error) {}
}

function createNo() {
    var now = new Date()
    var month = now.getMonth() + 1
    var day = now.getDate()
    var hour = now.getHours()
    var minutes = now.getMinutes()
    var seconds = now.getSeconds()
    // month = now.setMonth(month)
    month = month.toString().padStart(2, '0')
    day = day.toString().padStart(2, '0')
    // hour = now.setHours(hour)
    hour = hour.toString().padStart(2, '0')

    // minutes = now.setMinutes(minutes)
    minutes = minutes.toString().padStart(2, '0')
    // seconds = now.setSeconds(seconds)
    seconds = seconds.toString().padStart(2, '0')
    var time = now.getFullYear().toString() + month + day + hour + minutes + seconds
    return 'T' + (time + (Math.floor((Math.random() * 1000000)).toString()).padStart(6, '9'))
}